const express = require("express");
const db = require("../../utils/db");
const router = express.Router();

// 获取数据
router.post("/tig_getDb", (req, res) => {
  const sql = `select * from closecontact_table`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ err, code: 1, msg: "请求出错" });
    } else {
      res.send({ code: 0, data, msg: "请求成功" });
    }
  });
});
// 添加数据
router.post("/tig_addDb",(req,res)=>{
    const {con_id,name,subPeople_count,sex,id_card,phoneNum,now_place,relational_cases,type,registrant,registrant_date,contact_state} = req.body
    const sql=`INSERT INTO closecontact_table (con_id,name,subPeople_count,sex,id_card,phoneNum,now_place,relational_cases,type,registrant,registrant_date,contact_state) 
    VALUES('${con_id}','${name}','${subPeople_count}','${sex}','${id_card}','${phoneNum}','${now_place}','${relational_cases}','${type}','${registrant}','${registrant_date}','${contact_state}')`
    db.query(sql,(err,data)=>{
        if(err){
            res.send({code:1,err,msg:'请求失败'})
        }else{
            res.send({code:0,data,msg:'添加成功'})
        }
    })
})
// 查询
router.post("/tig_searchDb", (req, res) => {
    const {
    name,
    sex,
    id_card,
    phoneNum,
    now_place,
    registrant,
    contact_type,
    contact_state,
    registrant_date
    } = req.body;
    const sql = `SELECT * FROM closecontact_table 
            WHERE name LIKE '%${name}%'  
            AND sex LIKE '%${sex}%'
            AND id_card LIKE '%${id_card}%'
            AND phoneNum LIKE '%${phoneNum}%'
            AND now_place LIKE '%${now_place}%'
            AND contact_type LIKE '%${contact_type}%'
            AND contact_state LIKE '%${contact_state}%'
            AND registrant LIKE '%${registrant}%'
            AND registrant_date LIKE '%${registrant_date}%'
            `;
    db.query(sql, (err, data) => {
      if (err) {
        res.send({ code: 1, msg: "请求出错" });
      } else {
        res.send({ code: 0, data, msg: "成功" });
      }
    });
  });
//   删除
router.post("/tig_delDb", (req, res) => {
    const { con_id } = req.body;
    const sql = `DELETE FROM closecontact_table WHERE con_id=${con_id}`;
    db.query(sql,(err,data)=>{
        if(err){
            res.send({err,code:1,msg:"请求出错"})
        }else{
            res.send({code:0,msg:'成功'})
        }
    })
})
// 编辑--次密接
router.post("/tig_editSubDb",(req,res)=>{
    const {con_id,relational_cases,name,sex,id_card,age,occupation,phoneNum,now_place,vaccine_situation,contact_mode,last_contact_time,exposure_type,control_date,control_situation,birth_date,contact_type,contact_state}=req.body
    const sql=`UPDATE closecontact_table SET  
	relational_cases='${relational_cases}',name='${name}',sex='${sex}',id_card='${id_card}',
	age='${age}',occupation='${occupation}',phoneNum='${phoneNum}',now_place='${now_place}',
	vaccine_situation='${vaccine_situation}',contact_mode='${contact_mode}',
	last_contact_time='${last_contact_time}',exposure_type='${exposure_type}',
	control_date='${control_date}',control_situation='${control_situation}',
    birth_date='${birth_date}',contact_type='${contact_type}',contact_state='${contact_state}'
	WHERE con_id = '${con_id}'`
    db.query(sql,(err,data)=>{
        if(err){
            res.send({err,code:1,msg:'请求错误'})
        }else{
            res.send({code:0,msg:'成功'})
        }
    })
})
// 编辑--密接/一般
router.post("/tig_editTigDb",(req,res)=>{
    const {con_id,relational_cases,name,sex,id_card,age,occupation,phoneNum,now_place,vaccine_situation,contact_mode,last_contact_time,exposure_type,control_date,control_situation,birth_date,home_Info,infected_person_relational,whether_symptom,whether_doctor,
        symptom,occurred_date,contact_type,contact_state}=req.body
    const sql=`UPDATE closecontact_table SET  
	relational_cases='${relational_cases}',name='${name}',sex='${sex}',id_card='${id_card}',
	age='${age}',occupation='${occupation}',phoneNum='${phoneNum}',now_place='${now_place}',
	vaccine_situation='${vaccine_situation}',contact_mode='${contact_mode}',
	last_contact_time='${last_contact_time}',exposure_type='${exposure_type}',
	control_date='${control_date}',control_situation='${control_situation}',
    birth_date='${birth_date}',home_Info='${home_Info}',
    infected_person_relational='${infected_person_relational}',
    whether_symptom='${whether_symptom}',whether_doctor='${whether_doctor}',
    symptom='${symptom}',occurred_date='${occurred_date}',contact_type='${contact_type}',
    contact_state='${contact_state}'
	WHERE con_id = '${con_id}'`
    db.query(sql,(err,data)=>{
        if(err){
            res.send({err,code:1,msg:'请求错误'})
        }else{
            res.send({code:0,msg:'成功'})
        }
    })
})
module.exports = router;
